package com.ews.onlineexchange.service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.alibaba.fastjson.JSONObject;
import com.ews.onlineexchange.app.entity.ServiceBuyerStat;
import com.ews.onlineexchange.app.entity.TempServiceSupplier;
import com.ews.onlineexchange.controller.ServiceBuyerListForRest;
import com.ews.onlineexchange.model.ServiceBuyer;
import com.ews.onlineexchange.model.ServiceSupplier;
@Stateless
@Named
public class ServiceBuyerService implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//发布起止日期
	private Date startDate;//
	private Date endDate;
	@Inject
	ServiceBuyerListForRest serviceBuyerListForRest;
	@Inject
	EntityManager entityManager;

	
	
	

//	//报名总次数
	private final static String GET_BULLETIN_BY_BUYER_ID = "	"
			+ " SELECT bulletin.price,bulletin.hasAppliedCount		"
			+ " FROM Bulletin bulletin								"
			+ " WHERE bulletin.delFlag = 0							"
			+ " AND bulletin.over = 1            					"
			+ " AND bulletin.tradeStatus = 'Agreed'       			"
			+ "	AND bulletin.buyer.id = :buyerId					" 
			+ " AND (bulletin.overTime >= :startDate OR :startDate IS NULL) "
			+ " AND (bulletin.overTime <= :endDate OR :endDate IS NULL) ";
	public List getBulletinByBuyerId(String buyerId,Date startDate,Date endDate) {
		return entityManager.createQuery(GET_BULLETIN_BY_BUYER_ID).setParameter("buyerId", buyerId)
							.setParameter("startDate", startDate).setParameter("endDate", endDate).getResultList();
	}
	public JSONObject stat(Integer pageSize,Integer pageNumber, Date startDate,Date endDate,String buyerName){
		JSONObject json = new JSONObject();
		
		long start = System.currentTimeMillis();
		serviceBuyerListForRest.refresh();
		serviceBuyerListForRest.setBuyerName(buyerName);
		if(pageSize != null)serviceBuyerListForRest.setFirstResult((pageNumber - 1) * pageSize);
		serviceBuyerListForRest.setMaxResults(pageSize);
		List<ServiceBuyer> buyers = serviceBuyerListForRest.getResultList();		
		List<ServiceBuyerStat> stats = new ArrayList<ServiceBuyerStat>();
		for (ServiceBuyer buyer : buyers) {
			ServiceBuyerStat stat = new ServiceBuyerStat();
			stat.setBuyerName(buyer.getCnname());
			String buyerId = buyer.getId();
			List resultList = getBulletinByBuyerId(buyerId,startDate,endDate);
			stat.setBulletinCount(resultList.size());
			BigDecimal totalPrice = new BigDecimal(0);
			Integer totalAppliedCount = 0;
			for (Object object : resultList) {
				Object[] array = (Object[])object;
				BigDecimal price = (BigDecimal)array[0] ;
				if(price == null) price = new BigDecimal(0);
				Integer appliedCount = (Integer)array[1];
				totalPrice = totalPrice.add(price);
				totalAppliedCount += appliedCount;
			}	
			stat.setTotalPrice(totalPrice);
			stat.setAppliedCount(totalAppliedCount);
			stats.add(stat);
		}	
		json.put("totalCount", serviceBuyerListForRest.getResultCount());
		json.put("list", stats);
		long end = System.currentTimeMillis();
		System.out.println("共用时间："+(end - start) /1000);
		return json;
	}
	
	
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	private static final String EXIST_CREDITCODE_EQL = ""
			+ "	SELECT			count(user)"
			+ "	FROM			ServiceBuyer user"
			+ "	WHERE			user.creditCode = :creditCode	";
	public boolean existCreditcode(String creditCode) {
		Query query = entityManager.createQuery(EXIST_CREDITCODE_EQL);
		query.setParameter("creditCode", creditCode);
		long count = (long)query.getSingleResult();
		if (count > 0) return true;
		else return false;
	}
	
	
	
	
}
